import fs from 'fs';
import http from 'http';
import url from 'url';

let port=8080;
let ab=http.createServer((req,res)=>{
    console.log(url.format(req.url));
    let apath=req.url;
    let data=ff(apath);
    res.writeHead(200,{'content-type': 'text/html;charset=utf-8'});
    res.end(data);
});

ab.listen(port,()=>{
    console.log("监听成功");
})
console.log(`服务器运行在http://localhost:${port}`);

function ff(filepath){
    let res='很遗憾';
    console.log(filepath);
    if(filepath !=='./favicon.ico')
    {
    let tpath='.'+filepath;
    let hpath='.'+filepath+'/'+'index.html';
    let tt=fs.existsSync(hpath);
    if(tt){
        res=fs.readFileSync(hpath,'utf-8');
    }else{
        let arr=fs.readdirSync(tpath);
        arr.forEach((item)=>{
            let dpath='.'+filepath+'/'+item;
            if(dpath.isDirectory()){
                res=ff(filepath+'/'+item);
            }
        })
    }
 }else{
    console.log('ignore');
 }
    return res;
}